package com.guarantee.server.common.utils;

import cn.hutool.core.convert.Convert;

import java.math.BigDecimal;
import java.text.MessageFormat;

import static cn.hutool.core.util.StrUtil.EMPTY;

/**
 * @Description 字符串工具类
 * @Author chenli
 * @Date 2019/7/23
 * @Version Copyright (c) 2019,安徽阡陌网络科技有限公司 All rights reserved.
 */
public class AmountUtils {

    /**
     * 替换占位符
     */
    private static final String PLACEHOLDER = "*****************************************************************";

    public static boolean isEmpty(String str) {
        return str == null || str.length() == 0;
    }

    /**
     * <p>Checks if a String is not empty ("") and not null.</p>
     *
     * <pre>
     * StringUtils.isNotEmpty(null)      = false
     * StringUtils.isNotEmpty("")        = false
     * StringUtils.isNotEmpty(" ")       = true
     * StringUtils.isNotEmpty("bob")     = true
     * StringUtils.isNotEmpty("  bob  ") = true
     * </pre>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is not empty and not null
     */
    public static boolean isNotEmpty(String str) {
        return !StringUtils.isEmpty(str);
    }

    /**
     * <p>Checks if a String is whitespace, empty ("") or null.</p>
     *
     * <pre>
     * StringUtils.isBlank(null)      = true
     * StringUtils.isBlank("")        = true
     * StringUtils.isBlank(" ")       = true
     * StringUtils.isBlank("bob")     = false
     * StringUtils.isBlank("  bob  ") = false
     * </pre>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is null, empty or whitespace
     * @since 2.0
     */
    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    /**
     * <p>Checks if a String is not empty (""), not null and not whitespace only.</p>
     *
     * <pre>
     * StringUtils.isNotBlank(null)      = false
     * StringUtils.isNotBlank("")        = false
     * StringUtils.isNotBlank(" ")       = false
     * StringUtils.isNotBlank("bob")     = true
     * StringUtils.isNotBlank("  bob  ") = true
     * </pre>
     *
     * @param str  the String to check, may be null
     * @return <code>true</code> if the String is
     *  not empty and not null and not whitespace
     * @since 2.0
     */
    public static boolean isNotBlank(String str) {
        return !StringUtils.isBlank(str);
    }

    // Trim
    //-----------------------------------------------------------------------
    /**
     * <p>Removes control characters (char &lt;= 32) from both
     * ends of this String, handling <code>null</code> by returning
     * an empty String ("").</p>
     *
     * <pre>
     * StringUtils.clean(null)          = ""
     * StringUtils.clean("")            = ""
     * StringUtils.clean("abc")         = "abc"
     * StringUtils.clean("    abc    ") = "abc"
     * StringUtils.clean("     ")       = ""
     * </pre>
     *
     * @see String#trim()
     * @param str  the String to clean, may be null
     * @return the trimmed text, never <code>null</code>
     * @deprecated Use the clearer named {@link #trimToEmpty(String)}.
     *             Method will be removed in Commons Lang 3.0.
     */
    public static String clean(String str) {
        return str == null ? EMPTY : str.trim();
    }

    /**
     * <p>Removes control characters (char &lt;= 32) from both
     * ends of this String, handling <code>null</code> by returning
     * <code>null</code>.</p>
     *
     * <p>The String is trimmed using {@link String#trim()}.
     * Trim removes start and end characters &lt;= 32.
     *
     * <p>To trim your choice of characters, use the
     *
     * <pre>
     * StringUtils.trim(null)          = null
     * StringUtils.trim("")            = ""
     * StringUtils.trim("     ")       = ""
     * StringUtils.trim("abc")         = "abc"
     * StringUtils.trim("    abc    ") = "abc"
     * </pre>
     *
     * @param str  the String to be trimmed, may be null
     * @return the trimmed string, <code>null</code> if null String input
     */
    public static String trim(String str) {
        return str == null ? null : str.trim();
    }

    /**
     * <p>Removes control characters (char &lt;= 32) from both
     * ends of this String returning <code>null</code> if the String is
     * empty ("") after the trim or if it is <code>null</code>.
     *
     * <p>The String is trimmed using {@link String#trim()}.
     * Trim removes start and end characters &lt;= 32.
     *
     * <pre>
     * StringUtils.trimToNull(null)          = null
     * StringUtils.trimToNull("")            = null
     * StringUtils.trimToNull("     ")       = null
     * StringUtils.trimToNull("abc")         = "abc"
     * StringUtils.trimToNull("    abc    ") = "abc"
     * </pre>
     *
     * @param str  the String to be trimmed, may be null
     * @return the trimmed String,
     *  <code>null</code> if only chars &lt;= 32, empty or null String input
     * @since 2.0
     */
    public static String trimToNull(String str) {
        String ts = trim(str);
        return isEmpty(ts) ? null : ts;
    }

    /**
     * <p>Removes control characters (char &lt;= 32) from both
     * ends of this String returning an empty String ("") if the String
     * is empty ("") after the trim or if it is <code>null</code>.
     *
     * <p>The String is trimmed using {@link String#trim()}.
     * Trim removes start and end characters &lt;= 32.
     *
     * <pre>
     * StringUtils.trimToEmpty(null)          = ""
     * StringUtils.trimToEmpty("")            = ""
     * StringUtils.trimToEmpty("     ")       = ""
     * StringUtils.trimToEmpty("abc")         = "abc"
     * StringUtils.trimToEmpty("    abc    ") = "abc"
     * </pre>
     *
     * @param str  the String to be trimmed, may be null
     * @return the trimmed String, or an empty String if <code>null</code> input
     * @since 2.0
     */
    public static String trimToEmpty(String str) {
        return str == null ? EMPTY : str.trim();
    }




    /**
     * <p>Check if a String starts with a specified prefix.</p>
     *
     * <p><code>null</code>s are handled without exceptions. Two <code>null</code>
     * references are considered to be equal. The comparison is case sensitive.</p>
     *
     * <pre>
     * StringUtils.startsWith(null, null)      = true
     * StringUtils.startsWith(null, "abc")     = false
     * StringUtils.startsWith("abcdef", null)  = false
     * StringUtils.startsWith("abcdef", "abc") = true
     * StringUtils.startsWith("ABCDEF", "abc") = false
     * </pre>
     *
     * @see String#startsWith(String)
     * @param str  the String to check, may be null
     * @param prefix the prefix to find, may be null
     * @return <code>true</code> if the String starts with the prefix, case sensitive, or
     *  both <code>null</code>
     * @since 2.4
     */
    public static boolean startsWith(String str, String prefix) {
        return startsWith(str, prefix, false);
    }

    /**
     * <p>Case insensitive check if a String starts with a specified prefix.</p>
     *
     * <p><code>null</code>s are handled without exceptions. Two <code>null</code>
     * references are considered to be equal. The comparison is case insensitive.</p>
     *
     * <pre>
     * StringUtils.startsWithIgnoreCase(null, null)      = true
     * StringUtils.startsWithIgnoreCase(null, "abc")     = false
     * StringUtils.startsWithIgnoreCase("abcdef", null)  = false
     * StringUtils.startsWithIgnoreCase("abcdef", "abc") = true
     * StringUtils.startsWithIgnoreCase("ABCDEF", "abc") = true
     * </pre>
     *
     * @see String#startsWith(String)
     * @param str  the String to check, may be null
     * @param prefix the prefix to find, may be null
     * @return <code>true</code> if the String starts with the prefix, case insensitive, or
     *  both <code>null</code>
     * @since 2.4
     */
    public static boolean startsWithIgnoreCase(String str, String prefix) {
        return startsWith(str, prefix, true);
    }

    /**
     * <p>Check if a String starts with a specified prefix (optionally case insensitive).</p>
     *
     * @see String#startsWith(String)
     * @param str  the String to check, may be null
     * @param prefix the prefix to find, may be null
     * @param ignoreCase inidicates whether the compare should ignore case
     *  (case insensitive) or not.
     * @return <code>true</code> if the String starts with the prefix or
     *  both <code>null</code>
     */
    private static boolean startsWith(String str, String prefix, boolean ignoreCase) {
        if (str == null || prefix == null) {
            return (str == null && prefix == null);
        }
        if (prefix.length() > str.length()) {
            return false;
        }
        return str.regionMatches(ignoreCase, 0, prefix, 0, prefix.length());
    }

    /**
     * 手机号加*
     *
     * @param mobile :
     * @return ： 139****8888
     */
    public static String mobilePlaceholder(String mobile) {
        return mobile.replaceAll("(\\S{3})\\S{4}(\\S{4})", "$1****$2");
    }

    /**
     * 替换字符串
     *
     * @param str   ：字符串
     * @param start ：从第几位开始替换，0表示第一位
     * @return ：返回替换后位置
     */
    public static String stringPlaceholder(String str, int start) {
        if (isBlank(str)) {
            return str;
        }
        int length = str.length();
        if (length < start) {
            return str;
        }
        return str.replaceAll("(\\S{" + start + "})\\S{" + (length - start) + "}", "$1" + PLACEHOLDER.substring(0, (length - start)));

    }

    /**
     * 替换字符串
     *
     * @param str   ：字符串
     * @param start ：从第几位开始替换，0表示第一位
     * @param end   ：末尾留下几位
     * @return ：返回替换后位置
     */
    public static String stringPlaceholder(String str, int start, int end) {
        if (isBlank(str)) {
            return str;
        }
        int length = str.length();
        if (length < start) {
            return str;
        }
        if (length < (start + end)) {
            return stringPlaceholder(str, start);
        }
        return str.replaceAll("(\\S{" + start + "})\\S{" + (length - start - end) + "}(\\S{" + end + "})", "$1" + PLACEHOLDER.substring(0, (length - start - end)) + "$2");

    }

    public static String getBigString(String str) {
        String bigMoney = Convert.digitToChinese(new BigDecimal(str));
        System.out.println("转换前:"+str+"转换后:"+bigMoney);
        return bigMoney;
    }

    private static String getNumer(int size,int num,String fin) {
        String str = "";
        switch(size){
            case 1:
                if(num > 0) {
                    str = getNumStr(num);
                }else {
                    str = "";
                }
                break;
            case 2:
                if(num > 0) {
                    str = getNumStr(num) + "拾";
                }else {
                    // 前面不是零，加个零
                    str = addZero(fin);
                }
                break;
            case 3:
                if(num > 0) {
                    str = getNumStr(num) + "佰";
                }else {
                    // 前面不是零，加个零
                    str = addZero(fin);
                }
                break;
            case 4:
                if(num > 0) {
                    str = getNumStr(num) + "仟";
                }else {
                    // 前面不是零，加个零
                    str = addZero(fin);
                }
                break;
            case 5:
                if(num > 0) {
                    str = getNumStr(num) + "万";
                }else {
                    str = "万";
                }
                break;
            case 6:
                if(num > 0) {
                    str = getNumStr(num) + "拾";
                }
                break;
            case 7:
                if(num > 0) {
                    str = getNumStr(num) + "佰万";
                }else {
                    // 前面不是零，加个零
                    str = addZero(fin);
                }
                break;
            case 8:
                if(num > 0) {
                    str = getNumStr(num) + "仟万";
                }else {
                    // 前面不是零，加个零
                    str = addZero(fin);
                }
                break;

        }
        return str;
    }


    private static String addZero(String fin) {
        if(!fin.endsWith("零")) {
            return "零";
        }else {
            return "";
        }
    }

    private static String getNumStr(int num) {
        String str = "壹";
        switch(num) {
            case 2:
                str = "贰";
                break;
            case 3:
                str = "叁";
                break;
            case 4:
                str = "肆";
                break;
            case 5:
                str = "伍";
                break;
            case 6:
                str = "陆";
                break;
            case 7:
                str = "柒";
                break;
            case 8:
                str = "捌";
                break;
            case 9:
                str = "玖";
                break;
            case 0:
                str = "零";
                break;
        }
        return str;
    }

    /**
     *
     * @param template
     * @param values
     * @return
     */
    public static String parseNoticeTemplate(String template,String[] values){

        return "";
    }

    public static void main(String[] args) {
        String template = "我决定了款{0},lkajsd{1}";
        String[] fill = new String[]{"123","456"};
        String value = MessageFormat.format(template,new String[]{"123","456"});
        System.out.println(value);
    }

}